package com.meida.module.system.provider.service.impl;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.*;
import com.meida.common.mybatis.query.CriteriaDelete;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.system.client.entity.SysDict;
import com.meida.module.system.client.entity.SysDictData;
import com.meida.module.system.provider.mapper.SysDictDataMapper;
import com.meida.module.system.provider.service.SysDictDataService;
import com.meida.module.system.provider.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典数据接口实现
 *
 * @author flyme
 */
@Slf4j
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Autowired
    private SysDictService dictService;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<SysDictData> findByDictId(Long dictId) {
        CriteriaQuery<SysDictData> cq = new CriteriaQuery(SysDictData.class);
        cq.select("dictDataId", "dicDataTitle", "dicDataValue");
        cq.eq("dictId", dictId).eq("state", CommonConstants.ENABLED).orderByAsc("sortOrder");
        return list(cq);
    }

    @Override
    public void deleteByDictId(Long dictId) {
        CriteriaQuery cq = new CriteriaQuery(SysDictData.class);
        cq.eq("dictId", dictId);
        remove(cq);
    }

    /**
     * 根据上级id查询
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> selectByDictId(Long id) {
        CriteriaQuery cq = new CriteriaQuery(SysDictData.class);
        cq.select("dicDataTitle name,dicDataValue code").eq("dictId", id);
        cq.eq("state", CommonConstants.ENABLED_STR).orderByAsc("sortOrder");
        return selectEntityMap(cq);
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public EntityMap selectByTypes(String[] dictTypes) {
        EntityMap map = new EntityMap();
        if (FlymeUtils.isNotEmpty(dictTypes)) {
            for (String type : dictTypes) {
                SysDict sysDict = dictService.findByType(type);
                if (FlymeUtils.isNotEmpty(sysDict)) {
                    List<EntityMap> maps = selectByDictId(sysDict.getDictId());
                    map.put(type, maps);
                }
            }
        }
        return map;
    }

    @Override
    public SysDictData findByTypeAndValue(String dictType, Object dictDataValue) {
        SysDict dict = dictService.findByType(dictType);
        if (FlymeUtils.isNotEmpty(dict)) {
            CriteriaQuery cq = new CriteriaQuery(SysDictData.class);
            cq.eq(true, "dictId", dict.getDictId());
            cq.eq(true, "dicDataValue", dictDataValue);
            return getOne(cq);
        }
        return null;
    }

    @Override
    public ResultBody add(SaveModel model) {
        ResultBody resultBody;
        CriteriaSave cs = new CriteriaSave(model, SysDictData.class);
        String dicDataValue = cs.getParams("dicDataValue");
        cs.setHandlerName("dictSaveHandler");
        SysDictData dictData = cs.getEntity();
        SysDict dict = dictService.getById(dictData.getDictId());
        if (dict == null) {
            return ResultBody.failed("字典类型id不存在");
        }
        if (FlymeUtils.isEmpty(dicDataValue)) {
            dicDataValue = IdWorker.getIdStr();
            cs.put("dicDataValue", dicDataValue);
        }

        resultBody = baseAdd(cs);
        dictService.initDictRedis();
        return resultBody;
    }

    @Override
    public ResultBody edit(UpdateModel model) {
        ResultBody resultBody;
        CriteriaUpdate cu = new CriteriaUpdate(model, SysDictData.class);
        cu.setHandlerName("dictUpdateHandler");
        resultBody = baseEdit(cu);
        dictService.initDictRedis();
        return resultBody;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody pageList(PageModel pageModel) {
        CriteriaQuery cq = new CriteriaQuery(pageModel, entityClass);
        cq.select(SysDictData.class, "*");
        cq.eq(entityClass, "dictId");
        return basePageList(cq);
    }

    @Override
    public ResultBody delete(DeleteModel model) {
        ResultBody resultBody;
        CriteriaDelete cd = new CriteriaDelete(model, entityClass);
        resultBody = baseDelete(cd);
        dictService.initDictRedis();
        return resultBody;
    }

    /**
     * 状态更新完成后刷新redis
     *
     * @param cq
     * @return
     */
    @Override
    public ResultBody afterSetState(CriteriaUpdate cq) {
        dictService.initDictRedis();
        return ResultBody.ok();
    }

}